/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.common.ReflectUtils;
import com.fujitsu.nanjing.tieba.domain.SectionInfo;
import com.fujitsu.nanjing.tieba.ibator.CoreSection;
import com.fujitsu.nanjing.tieba.ibator.CoreSectionCriteria;
import com.fujitsu.nanjing.tieba.model.Section;
/**
 * SectionInfoService.java
 * 
 * @author Qixiaobo-win8
 */
@Service
@Transactional
public class SectionInfoService
		extends
			BaseService<SectionInfo, CoreSectionCriteria> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(SectionInfoService.class);

	@Resource
	private Section section;
	@Resource
	private SectionService sectionService;
	@Resource
	private BoardService boardService;

	/**
	 * 
	 * 查找分区同时将板块填充进去
	 * */
	@Override
	public SectionInfo findById(String id) {
		CoreSection coreSection = section.selectByPrimaryKey(Integer
				.parseInt(id));
		// 如果是查到为空,则返回空,不执行复制操作
		if (coreSection == null) {
			return null;
		}
		SectionInfo info = new SectionInfo();

		try {
			ReflectUtils.copy(coreSection, info);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		info.setBoardsBloBs(boardService.findAllBySectionId(info.getSectionid()
				.toString()));
		return info;

	}
	@Override
	public SectionInfo insert(SectionInfo t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(SectionInfo t) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(SectionInfo t) {
		// TODO Auto-generated method stub

	}

	public List<SectionInfo> findAll() {
		List<SectionInfo> sectionInfos = new ArrayList<SectionInfo>();
		List<CoreSection> sections = sectionService.findAll();
		SectionInfo info = null;
		for (CoreSection section : sections) {
			info = findById(section.getSectionid().toString());
			sectionInfos.add(info);
		}
		return sectionInfos;
	}

}
